Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / app / src / main / java / com / geeksville / mesh / repository / location / LocationRepository.kt

Displaying Raw • Download

app/src/main/java/com/geeksville/mesh/repository/location/LocationRepository.kt ui_message_list (be3f882f) Text, 4.80 KB

T8b949e/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

Tff7b72package T7ee787com.geeksville.mesh.repository.location

Tff7b72import T7ee787android.Manifest.permission.ACCESS_COARSE_LOCATION
Tff7b72import T7ee787android.Manifest.permission.ACCESS_FINE_LOCATION
Tff7b72import T7ee787android.app.Application
Tff7b72import T7ee787android.location.LocationManager
Tff7b72import T7ee787androidx.annotation.RequiresPermission
Tff7b72import T7ee787androidx.core.location.LocationCompat
Tff7b72import T7ee787androidx.core.location.LocationListenerCompat
Tff7b72import T7ee787androidx.core.location.LocationManagerCompat
Tff7b72import T7ee787androidx.core.location.LocationRequestCompat
Tff7b72import T7ee787androidx.core.location.altitude.AltitudeConverterCompat
Tff7b72import T7ee787com.geeksville.mesh.android.GeeksvilleApplication
Tff7b72import T7ee787com.geeksville.mesh.android.Logging
Tff7b72import T7ee787kotlinx.coroutines.Dispatchers
Tff7b72import T7ee787kotlinx.coroutines.asExecutor
Tff7b72import T7ee787kotlinx.coroutines.channels.awaitClose
Tff7b72import T7ee787kotlinx.coroutines.flow.MutableStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.StateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.callbackFlow
Tff7b72import T7ee787javax.inject.Inject
Tff7b72import T7ee787javax.inject.Singleton

Tf0883e@Singleton
Tff7b72class T56d364LocationRepository Tf0883e@Inject Tff7b72constructorTb4b4b4(
Tff7b72private Tff7b72val Te6edf3contextTb4b4b4: Te6edf3ApplicationTb4b4b4,
Tff7b72private Tff7b72val Te6edf3locationManagerTb4b4b4: Te6edf3daggerTb4b4b4.Te6edf3LazyTff7b72<Te6edf3LocationManagerTff7b72>Tb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3Logging Tb4b4b4{

T8b949e/**
* Status of whether the app is actively subscribed to location changes.
*/
Tff7b72private Tff7b72val Te6edf3_receivingLocationUpdatesTb4b4b4: Te6edf3MutableStateFlowTff7b72<Tffa657BooleanTff7b72> Tff7b72= Te6edf3MutableStateFlowTb4b4b4(Tff7b72falseTb4b4b4)
Tff7b72val Te6edf3receivingLocationUpdatesTb4b4b4: Te6edf3StateFlowTff7b72<Tffa657BooleanTff7b72> Tff7b72getTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3_receivingLocationUpdates

Tf0883e@RequiresPermissionTb4b4b4(Te6edf3anyOf Tff7b72= Tff7b72[Te6edf3ACCESS_COARSE_LOCATIONTb4b4b4, Te6edf3ACCESS_FINE_LOCATIONTff7b72]Tb4b4b4)
Tff7b72private Tff7b72fun Te6edf3LocationManagerTb4b4b4.Td2a8ffrequestLocationUpdatesTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3callbackFlow Tb4b4b4{

Tff7b72val Te6edf3intervalMs Tff7b72= T79c0ff3T79c0ff0 Tff7b72* T79c0ff1T79c0ff0T79c0ff0T79c0ff0L T8b949e// 30 seconds
Tff7b72val Te6edf3minDistanceM Tff7b72= T79c0ff0f

Tff7b72val Te6edf3locationRequest Tff7b72= Te6edf3LocationRequestCompatTb4b4b4.Te6edf3BuilderTb4b4b4(Te6edf3intervalMsTb4b4b4)
Tb4b4b4.Te6edf3setMinUpdateDistanceMetersTb4b4b4(Te6edf3minDistanceMTb4b4b4)
Tb4b4b4.Te6edf3setQualityTb4b4b4(Te6edf3LocationRequestCompatTb4b4b4.Te6edf3QUALITY_HIGH_ACCURACYTb4b4b4)
Tb4b4b4.Te6edf3buildTb4b4b4(Tb4b4b4)

Tff7b72val Te6edf3locationListener Tff7b72= Te6edf3LocationListenerCompat Tb4b4b4{ Te6edf3location Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3locationTb4b4b4.Te6edf3hasAltitudeTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Tff7b72!Te6edf3LocationCompatTb4b4b4.Te6edf3hasMslAltitudeTb4b4b4(Te6edf3locationTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72try Tb4b4b4{
Te6edf3AltitudeConverterCompatTb4b4b4.Te6edf3addMslAltitudeToLocationTb4b4b4(Te6edf3contextTb4b4b4, Te6edf3locationTb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3ExceptionTb4b4b4) Tb4b4b4{
Te6edf3errormsgTb4b4b4(Ta5d6ff"Ta5d6ffaddMslAltitudeToLocation() failedTa5d6ff"Tb4b4b4, Te6edf3eTb4b4b4)
Tb4b4b4}
Tb4b4b4}
T8b949e// info("New location: $location")
Te6edf3trySendTb4b4b4(Te6edf3locationTb4b4b4)
Tb4b4b4}

Tff7b72val Te6edf3providerList Tff7b72= Te6edf3buildList Tb4b4b4{
Tff7b72val Te6edf3providers Tff7b72= Te6edf3allProviders
Tff7b72if Tb4b4b4(Te6edf3androidTb4b4b4.Te6edf3osTb4b4b4.Te6edf3BuildTb4b4b4.Te6edf3VERSIONTb4b4b4.Te6edf3SDK_INT Tff7b72>Tff7b72= T79c0ff3T79c0ff1 Tff7b72&Tff7b72& Te6edf3LocationManagerTb4b4b4.Te6edf3FUSED_PROVIDER Tff7b72in Te6edf3providersTb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3FUSED_PROVIDERTb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3GPS_PROVIDER Tff7b72in Te6edf3providersTb4b4b4) Te6edf3addTb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3GPS_PROVIDERTb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3NETWORK_PROVIDER Tff7b72in Te6edf3providersTb4b4b4) Te6edf3addTb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3NETWORK_PROVIDERTb4b4b4)
Tb4b4b4}
Tb4b4b4}

Te6edf3infoTb4b4b4(Ta5d6ff"Ta5d6ffStarting location updates with Tffd700$Te6edf3providerListTa5d6ff intervalMs=Tffd700${Te6edf3intervalMsTffd700}Ta5d6ffms and minDistanceM=Tffd700${Te6edf3minDistanceMTffd700}Ta5d6ffmTa5d6ff"Tb4b4b4)
Te6edf3_receivingLocationUpdatesTb4b4b4.Te6edf3value Tff7b72= Tff7b72true
Te6edf3GeeksvilleApplicationTb4b4b4.Te6edf3analyticsTb4b4b4.Te6edf3trackTb4b4b4(Ta5d6ff"Ta5d6fflocation_startTa5d6ff"Tb4b4b4) T8b949e// Figure out how many users needed to use the phone GPS

Tff7b72try Tb4b4b4{
Te6edf3providerListTb4b4b4.Te6edf3forEach Tb4b4b4{ Te6edf3provider Tff7b72-Tff7b72>
Te6edf3LocationManagerCompatTb4b4b4.Te6edf3requestLocationUpdatesTb4b4b4(
Tff7b72thisTf0883e@requestLocationUpdatesTb4b4b4,
Te6edf3providerTb4b4b4,
Te6edf3locationRequestTb4b4b4,
Te6edf3DispatchersTb4b4b4.Te6edf3IOTb4b4b4.Te6edf3asExecutorTb4b4b4(Tb4b4b4)Tb4b4b4,
Te6edf3locationListenerTb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3ExceptionTb4b4b4) Tb4b4b4{
Te6edf3closeTb4b4b4(Te6edf3eTb4b4b4) T8b949e// in case of exception, close the Flow
Tb4b4b4}

Te6edf3awaitClose Tb4b4b4{
Te6edf3infoTb4b4b4(Ta5d6ff"Ta5d6ffStopping location requestsTa5d6ff"Tb4b4b4)
Te6edf3_receivingLocationUpdatesTb4b4b4.Te6edf3value Tff7b72= Tff7b72false
Te6edf3GeeksvilleApplicationTb4b4b4.Te6edf3analyticsTb4b4b4.Te6edf3trackTb4b4b4(Ta5d6ff"Ta5d6fflocation_stopTa5d6ff"Tb4b4b4)

Te6edf3LocationManagerCompatTb4b4b4.Te6edf3removeUpdatesTb4b4b4(Tff7b72thisTf0883e@requestLocationUpdatesTb4b4b4, Te6edf3locationListenerTb4b4b4)
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Observable flow for location updates
*/
Tf0883e@RequiresPermissionTb4b4b4(Te6edf3anyOf Tff7b72= Tff7b72[Te6edf3ACCESS_COARSE_LOCATIONTb4b4b4, Te6edf3ACCESS_FINE_LOCATIONTff7b72]Tb4b4b4)
Tff7b72fun Td2a8ffgetLocationsTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3locationManagerTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3requestLocationUpdatesTb4b4b4(Tb4b4b4)
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.07s